<template>
  <div>
    <el-dialog :title="fromTitle" :visible="formVisible">
      <el-form :model="form">
        <el-row :gutter="20">
          <!-- INPUT -->
          <el-col :span="12" v-for="item in data" :key="item.id">
            <el-form-item :label="item.label">
              <el-input v-model="item.content" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <!-- SELECT -->
          <el-col :span="12" v-for="item in data2" :key="item.label">
            <el-form-item :label="item.label">
              <el-select v-model="item.content" placeholder="请选择">
                <el-option
                  v-for="item2 in item.options"
                  :key="item2.label"
                  :label="item2.label"
                  :value="item2.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeForm">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Dialog",
  props: {
    title: String,
    dialogFormVisible: Boolean,
    formDataInput: Array,
    formDataSelect: Array,
  },
  data() {
    return {
      formVisible: this.dialogFormVisible,
      form: {},
      data: this.formDataInput,
      data2: this.formDataSelect,
      fromTitle: this.title,
    };
  },
  watch: {
    dialogFormVisible() {
      console.log(this.dialogFormVisible);
      this.formVisible = this.dialogFormVisible;
    },
  },
  methods: {
    closeForm() {
      console.log(this.data);
      this.formVisible = false;
      this.$emit("formVisible", this.formVisible);
      this.data.forEach((item, index) => {
        this.data[index].content = "";
      });
      this.data2.forEach((item, index) => {
        this.data2[index].content = "";
      });
    },
    add() {
      this.data.forEach((item, index) => {
        this.form[this.data[index].name] = this.data[index].content;
      });
      this.data2.forEach((item, index) => {
        this.form[this.data2[index].name] = this.data2[index].content;
      });
      this.$emit("form", this.form);
    },
  },
};
</script>

<style>
</style>